﻿Public Class frmAttentes
    Private szSQL As String
    Private rs As ADODB.Recordset
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Private Sub frmAttentes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' initialiser le combo Type examens
        CB_TypeExamen.SelectedIndex = 2
    End Sub

    Private Sub CB_TypeExamen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CB_TypeExamen.SelectedIndexChanged
        ' charger la liste sélectionnée
        InitListeAttente(0)
    End Sub

    Private Sub InitListeAttente(ByVal ix As Integer)

        Dim rsEleve As ADODB.Recordset = Nothing
        Dim nb As Integer = 0
        Dim HistoExamen As String

         ' effacer la listview
        LV_ATTENTES.Items.Clear()
        LV_ATTENTES.ListViewItemSorter = Nothing

        ' Lire la table Attentes
        szSQL = "CALL spGet_ATTENTES_byCAT('" & CB_TypeExamen.SelectedItem.ToString & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        Do While Not rs.EOF
            LV_ATTENTES.Items.Add(rs.Fields("ordre").Value)
            LV_ATTENTES.Items(nb).SubItems.Add(Format(rs.Fields("cree_le").Value, "dd/MM/yyyy"))
            LV_ATTENTES.Items(nb).SubItems.Add(rs.Fields("cd_cli").Value)
            LV_ATTENTES.Items(nb).SubItems.Add(rs.Fields("nom").Value)

            ' Lire la fiche Eleves
            szSQL = "CALL spGet_ELEVES_byCD_CLI(" & nv(rs.Fields("cd_cli").Value) & ")"
            GetRecordset(ctDSN_AEM, szSQL, rsEleve)
            If rsEleve.RecordCount > 0 Then
                ' calcule le solde
                LV_ATTENTES.Items(nb).SubItems.Add(Zero2Space(rsEleve.Fields("credit").Value - rsEleve.Fields("debit").Value))
                LV_ATTENTES.Items(nb).SubItems.Add(rsEleve.Fields("permis_demande").Value)
                LV_ATTENTES.Items(nb).SubItems.Add(nv(rsEleve.Fields("formule").Value))
                ' formater Histo examens
                HistoExamen = "T" & rsEleve.Fields("tr_t_nb").Value
                ' examen théorique obtenu ?
                If IsDate(rsEleve.Fields("tr_t_ok").Value) Then
                    HistoExamen = HistoExamen & "ok-"
                Else
                    HistoExamen = HistoExamen & "_-"
                End If
                HistoExamen = HistoExamen & "P" & rsEleve.Fields("tr_p_nb").Value
                If IsDate(rsEleve.Fields("tr_p_ok").Value) Then
                    HistoExamen = HistoExamen & "ok-"
                Else
                    HistoExamen = HistoExamen & "_-"
                End If
                HistoExamen = HistoExamen & "R" & rsEleve.Fields("tr_r_nb").Value
                If IsDate(rsEleve.Fields("tr_r_ok").Value) Then
                    HistoExamen = HistoExamen & "ok-"
                Else
                    HistoExamen = HistoExamen & "_-"
                End If
                LV_ATTENTES.Items(nb).SubItems.Add(HistoExamen)
                ' formater la date du sernier passage
                Select Case CB_TypeExamen.SelectedItem.ToString
                    Case "ETG"
                        HistoExamen = IfDate(rsEleve.Fields("tr_t_date").Value)
                    Case "B"
                        HistoExamen = IfDate(rsEleve.Fields("tr_p_date").Value)
                    Case Else     ' 2R ou GL
                        If IsDate(rsEleve.Fields("tr_r_date").Value) Then
                            HistoExamen = IfDate(rsEleve.Fields("tr_r_date").Value)
                        Else
                            HistoExamen = IfDate(rsEleve.Fields("tr_p_date").Value)
                        End If
                End Select
                LV_ATTENTES.Items(nb).SubItems.Add(HistoExamen)
            End If
            rsEleve.Close()
            ' Get the next record.
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        LB_NbLignes.Text = CStr(nb) & " lignes"

        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            SortListView(LV_ATTENTES, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_ATTENTES)

    End Sub

    Private Sub LV_ATTENTES_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_ATTENTES.ColumnClick
        SortListView(LV_ATTENTES, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column
    End Sub

    Private Sub LV_ATTENTES_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_ATTENTES.DoubleClick

        If LV_ATTENTES.SelectedItems.Count > 0 Then
            frmAttentesMAJ.pTYPE = CB_TypeExamen.SelectedItem.ToString
            frmAttentesMAJ.pCD_CLI = LV_ATTENTES.SelectedItems(0).SubItems(2).Text
            ' afficher les détails 
            frmAttentesMAJ.ShowDialog()
            ' réaffiche la viewlist
            InitListeAttente(LV_ATTENTES.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_ATTENTES_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_ATTENTES.KeyDown
        Select e.KeyCode
            Case Keys.Enter
                ' touche Enter = DoubleClick
                LV_ATTENTES_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)
        End Select

    End Sub

    Private Sub BO_EDITION_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_EDITION.Click
        ' afficher l'état
        FrmViewReport.pCodeEtat = 9
        FrmViewReport.pParamRPT = ""
        FrmViewReport.ShowDialog()
    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' Ajouter un élève en attente
        frmAttentesMAJ.pTYPE = CB_TypeExamen.SelectedItem.ToString
        frmAttentesMAJ.pCD_CLI = 0
        ' afficher les détails 
        frmAttentesMAJ.ShowDialog()
        ' réaffiche la viewlist
        InitListeAttente(LV_ATTENTES.Items.Count)
    End Sub

End Class